<template>
  <div>
    <p class="tip">
      通过设置 <table-api-link prop="edit-config"/>.<table-api-link prop="autoClear"/> 关闭默认的单元格清除激活行为<br>
      <span class="red">（注：如果设置为手动模式则不会自动关闭激活状态，需要手动调用 clearActived 关闭编辑状态）</span>
    </p>

    <vhb-table
      border
      show-overflow
      keep-source
      ref="xTable"
      :data="tableData"
      :edit-config="{ trigger: 'manual', mode: 'row', autoClear: false}">
      <vhb-column type="seq" width="60"></vhb-column>
      <vhb-column field="name" title="Name" :edit-render="{name: 'input', immediate: true}"></vhb-column>
      <vhb-column field="sex" title="Sex" :edit-render="{name: 'select', options: sexList}"></vhb-column>
      <vhb-column field="date" title="Date" :edit-render="{name: 'input', immediate: true, attrs: { type: 'date' }}"></vhb-column>
      <vhb-column field="num" title="Num" :edit-render="{name: 'input', immediate: true, attrs: {type: 'number'}}"></vhb-column>
      <vhb-column field="address" title="Address" :edit-render="{name: 'textarea'}"></vhb-column>
      <vhb-column title="操作">
        <template #default="{ row }">
          <template v-if="$refs.xTable.isActiveByRow(row)">
            <vhb-button @click="saveRowEvent(row)">保存</vhb-button>
            <vhb-button @click="cancelRowEvent(row)">取消</vhb-button>
          </template>
          <template v-else>
            <vhb-button @click="editRowEvent(row)">编辑</vhb-button>
          </template>
        </template>
      </vhb-column>
    </vhb-table>

    <p class="demo-code">{{ $t('app.body.button.showCode') }}</p>

    <pre>
      <pre-code class="xml">{{ demoCodes[0] }}</pre-code>
      <pre-code class="javascript">{{ demoCodes[1] }}</pre-code>
    </pre>
  </div>
</template>

<script>
import VHBTable from 'vhb-table'

export default {
  data () {
    return {
      tableData: [
        { id: 10001, name: 'Test1', role: 'Develop', sex: '0', age: 28, address: 'test abc' },
        { id: 10002, name: 'Test2', role: 'Test', sex: '1', age: 22, address: 'Guangzhou' },
        { id: 10003, name: 'Test3', role: 'PM', sex: '0', age: 32, address: 'Shanghai' },
        { id: 10004, name: 'Test4', role: 'Designer', sex: '1', age: 23, address: 'test abc' },
        { id: 10005, name: 'Test5', role: 'Develop', sex: '1', age: 30, address: 'Shanghai' },
        { id: 10006, name: 'Test6', role: 'Designer', sex: '1', age: 21, address: 'test abc' }
      ],
      sexList: [
        { label: '女', value: '0' },
        { label: '男', value: '1' }
      ],
      demoCodes: [
        `
        <vhb-table
          border
          show-overflow
          keep-source
          ref="xTable"
          :data="tableData"
          :edit-config="{ trigger: 'manual', mode: 'row', autoClear: false}">
          <vhb-column type="seq" width="60"></vhb-column>
          <vhb-column field="name" title="Name" :edit-render="{name: 'input', immediate: true}"></vhb-column>
          <vhb-column field="sex" title="Sex" :edit-render="{name: 'select', options: sexList}"></vhb-column>
          <vhb-column field="date" title="Date" :edit-render="{name: 'input', immediate: true, attrs: { type: 'date' }}"></vhb-column>
          <vhb-column field="num" title="Num" :edit-render="{name: 'input', immediate: true, attrs: {type: 'number'}}"></vhb-column>
          <vhb-column field="address" title="Address" :edit-render="{name: 'textarea'}"></vhb-column>
          <vhb-column title="操作">
            <template #default="{ row }">
              <template v-if="$refs.xTable.isActiveByRow(row)">
                <vhb-button @click="saveRowEvent(row)">保存</vhb-button>
                <vhb-button @click="cancelRowEvent(row)">取消</vhb-button>
              </template>
              <template v-else>
                <vhb-button @click="editRowEvent(row)">编辑</vhb-button>
              </template>
            </template>
          </vhb-column>
        </vhb-table>
        `,
        `
        import VHBTable from 'vhb-table'
        
        export default {
          data () {
            return {
              tableData: [
                { id: 10001, name: 'Test1', role: 'Develop', sex: '0', age: 28, address: 'test abc' },
                { id: 10002, name: 'Test2', role: 'Test', sex: '1', age: 22, address: 'Guangzhou' },
                { id: 10003, name: 'Test3', role: 'PM', sex: '0', age: 32, address: 'Shanghai' },
                { id: 10004, name: 'Test4', role: 'Designer', sex: '1', age: 23, address: 'test abc' },
                { id: 10005, name: 'Test5', role: 'Develop', sex: '1', age: 30, address: 'Shanghai' },
                { id: 10006, name: 'Test6', role: 'Designer', sex: '1', age: 21, address: 'test abc' }
              ],
              sexList: [
                { label: '女', value: '0' },
                { label: '男', value: '1' }
              ]
            }
          },
          methods: {
            editRowEvent (row) {
              const $table = this.$refs.xTable
              $table.setActiveRow(row)
            },
            saveRowEvent () {
              const $table = this.$refs.xTable
              $table.clearActived().then(() => {
                VHBTable.modal.alert('success')
              })
            },
            cancelRowEvent (row) {
              const $table = this.$refs.xTable
              $table.clearActived()
                .then(() => $table.revertData(row))
            }
          }
        }
        `
      ]
    }
  },
  methods: {
    editRowEvent (row) {
      const $table = this.$refs.xTable
      $table.setActiveRow(row)
    },
    saveRowEvent () {
      const $table = this.$refs.xTable
      $table.clearActived().then(() => {
        VHBTable.modal.alert('success')
      })
    },
    cancelRowEvent (row) {
      const $table = this.$refs.xTable
      $table.clearActived()
        .then(() => $table.revertData(row))
    }
  }
}
</script>
